package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.query.CommodityQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.ICommodityService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("commodity")
public class CommodityController {

    @Autowired
    private ICommodityService commodityService;


    @RequestMapping("list")
    public String list(@ModelAttribute("qo") CommodityQueryObject qo, Model model) {
        PageInfo pageInfo = commodityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "commodity/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Commodity commodity, MultipartFile file) {
        if (file != null && file.getSize() > 0L) {
            String url = UploadUtil.upload(file);
            commodity.setCoverUrl(url);
        }
        commodityService.saveOrUpdate(commodity);
        return new JsonResult();
    }
}
